package com.modudf.web.servlet.maskInfoServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.modudf.common.XML2BeanUtils;
import com.modudf.domain.MaskInfo;
import com.modudf.domain.ResultInfo;
import com.modudf.exception.XMLConvertException;
import com.modudf.service.MaskInfoManagerImpl;

public class AddMaskInfoServlet extends HttpServlet {

	private static final long serialVersionUID = 5896669913952204624L;
	private static final Log log = LogFactory.getLog(AddMaskInfoServlet.class);
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml;charset=UTF-8");
		MaskInfo maskInfo = new MaskInfo();
		maskInfo.setOwner(request.getParameter("owner"));
		maskInfo.setTabName(request.getParameter("tabName"));
		maskInfo.setTabColumn(request.getParameter("tabColumn"));
		maskInfo.setFuncName(request.getParameter("funcName"));
		maskInfo.setFuncParam(request.getParameter("funcParam"));
		maskInfo.setFuncComment(request.getParameter("funcComment"));
		ResultInfo result;
		try {
			MaskInfoManagerImpl.getInstance().addMaskInfo(maskInfo);
			result = new ResultInfo();
		} catch (Exception e) {
			log.fatal(e);
			result = new ResultInfo(-1, e.getMessage());
		}
		PrintWriter out = response.getWriter();
		try {
			out.println(XML2BeanUtils.bean2XmlString(result));
		} catch (XMLConvertException e) {
			e.printStackTrace();
		}
		out.close();
	}
}
